/*
 * Copyright 2013 University of Padua, Italy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package it.unipd.dei.bd.icineasti.servlet;

import it.unipd.dei.bd.icineasti.database.GetElencoPoltroneInSalaDatabase;
import it.unipd.dei.bd.icineasti.resource.Poltrona;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

/**
 * Elenca le poltrone presenti in una sala ritornando una tabella formattata
 * per consentire una rappresentazione grafica della sala.
 * 
 * @author Bardelle Renzo
 * @version 1.00
 */
public class GetElencoPoltroneInSalaServlet extends AbstractDatabaseServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse res)
			throws ServletException, IOException {

		res.setContentType("text/html");

		PrintWriter out = res.getWriter();
		
		int idSala = Integer.valueOf(req.getParameter("id_sala"));

		Connection con = null;

		try {
			con = DS.getConnection();
			
			List<Poltrona> poltrone = new GetElencoPoltroneInSalaDatabase(con, idSala).elencaPoltrone();
			
			if(poltrone.size() > 0)
			{
			
				String maxSRiga = poltrone.get(0).getFila();
				int maxColonna = poltrone.get(0).getNumero();
			
				for (Poltrona e : poltrone) {
					if(e.getFila().compareToIgnoreCase(maxSRiga) > 0)
					{
						maxSRiga = e.getFila();
					}
					if(e.getNumero() > maxColonna)
					{
						maxColonna = e.getNumero();
					}
				}
				
				int maxRiga = maxSRiga.toUpperCase().charAt(0)-64;
				
				boolean[] mappaPoltrone=new boolean[maxRiga*maxColonna];
				
				int r=0;
				int c=0;
				
				for (Poltrona e : poltrone) {
					r=e.getFila().toUpperCase().charAt(0)-64;
					c=e.getNumero();
					mappaPoltrone[(r-1)*maxColonna+(c-1)]=true;
				}
				
				for(r = 1; r <= maxRiga; r++)
				{	
					out.print("<tr>");
					for(c = 1; c <= maxColonna; c++)
					{
						out.print("<td id=\""+(char)(r+64)+c+"\" poltrona=\""+mappaPoltrone[(r-1)*maxColonna+(c-1)]+"\" onclick=\"change(this)\"></td>");
					}
					out.println("</tr>");
				}
			
			}
				
		} catch (NullPointerException e) {
			out.printf("<tr><td><font color=\"red\" size=\"+2\">Impossibile creare la connessione al database.</font></td></tr>");
		} catch (SQLException e) {
			out.printf(
					"<tr><td><font color=\"red\" size=\"+2\">Errore durante l'accesso al database: %s</font></td></tr>",
					e.getMessage());
		}
			
		out.flush();
		out.close();

	}

}
